import {StrUtil} from "../utils/StrUtil";

export class DateUtil {

    public static formatDate(date: Date, delimiter: string = ''): String {
        let pattern = "yyyy-MM-dd";
        if (!StrUtil.isBlank(delimiter)) {
            pattern = pattern.replace(/-/g, delimiter);
        }
        return DateUtil.format(date, pattern)
    }

    public static formatDateTime(date: Date, delimiterDate: string = '', delimiterTime:string=''): String {
        let pattern = "yyyy-MM-dd hh:mm:ss";
        if (!StrUtil.isBlank(delimiterDate)) {
            pattern = pattern.replace(/-/g, delimiterDate)
        }
        if (!StrUtil.isBlank(delimiterTime)) {
            pattern = pattern.replace(/-/g, delimiterTime);
        }
        return this.format(date, pattern);
    }

    public static format(date: Date, pattern: String): String {
        if (pattern.includes("yyyy")) {
            date.getFullYear()
            pattern = pattern.replace("yyyy", String(date.getFullYear()))
        }
        if (pattern.includes("MM")) {
            let month = date.getMonth();
            pattern = pattern.replace("MM", String(month + 1).padStart(2, "0"))
        }
        if (pattern.includes("dd")) {
            pattern = pattern.replace("dd", String(date.getDate()).padStart(2, "0"))
        }
        if (pattern.includes("hh")) {
            pattern = pattern.replace("hh", String(date.getHours()).padStart(2, "0"))
        }
        if (pattern.includes("mm")) {
            pattern = pattern.replace("mm", String(date.getMinutes()).padStart(2, "0"))
        }
        if (pattern.includes("ss")) {
            pattern = pattern.replace("ss", String(date.getSeconds()).padStart(2, "0"))
        }
        if (pattern.includes("SSS")) {
            pattern = pattern.replace("S", String(date.getMilliseconds()).padStart(3, "0"))
        }
        return pattern
    }
}